// pages/my/my.js
var httpTool = require('../../utils/network.js');
var util = require('../../utils/util.js');
const app = getApp();
var page = 1;
Page({
  data: {
    list: [],
    isLoad: 0,
    userId: '',
    commentList: [{
      isHide: true
    }, {
      isHide: true
    }]
  },
  // 弹出、隐藏评论框
  toggleComment: function(e) {
    let _index = e.currentTarget.dataset.index;
    this.data.commentList.forEach((item, index) => {
      let _key = 'commentList[' + index + '].isHide'
      this.setData({
        [_key]: _index == index ? !this.data.commentList[_index].isHide : true
      })
    })
  },
  // 划入空白区域隐藏评论框
  hideAllComment: function() {
    let _isRun = this.data.commentList.some((item) => {
      return !item.isHide
    })
    _isRun && this.data.commentList.forEach((item, index) => {
      let _key = 'commentList[' + index + '].isHide'
      this.setData({
        [_key]: true
      })
    })
  },
  //发布动态
  toPublishFeed: function () {
    util.navigateTo('../releasedynamic/releasedynamic');
  },
  dianZan: function() {
    console.log("点赞")
  },
  pingLun: function() {
    console.log("评论")
  },
  fenXiang: function() {
    console.log("分享")
  },
  onLoad: function(options) {
    // 生命周期函数--监听页面加载
    // util.fxAlert('此页面为示例页面，开发小伙伴正在开发中，敬请期待!')
    // return;
    this.setData({
      userId: app.userData.userId
    })

    this.getDynamicList();
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function() {
    // 生命周期函数--监听页面显示
    if (app.updateData.isUpdateDynamic) {
      page = 1
      this.getDynamicList()
      app.updateData.isUpdateDynamic = false
    }
  },
  onPullDownRefresh: function () {
    page = 1
    this.getDynamicList()
    wx.stopPullDownRefresh()
  },
  onReachBottom: function () {

    if (this.data.isLoad == 0) {
      page++
      this.getDynamicList()
    }

  },
  deleteClick: function (event) {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '是否删除当前动态?',
      success: function (res) {
        if (res.confirm) {
          var model = event.currentTarget.dataset.model;
          var index = event.currentTarget.dataset.index;
          console.log(index)
          console.log(model)

          var url = app.data.httpUrl + 'v1/shopFeed/delete.json'
          var param = {
            id: model.id,
            userId: app.userData.userId,
            shopId: model.shopId,
          }
          httpTool.sendRequest(url, param, '删除中...', function (res) {

            var tempList = that.data.list
            tempList.splice(index, 1)

            that.setData({
              list: tempList
            })
            console.log(that.data.list)
          }, function (error) {

          });
        } else if (res.cancel) {

        }
      }
    })



  },
  getDynamicList: function () {
    var that = this
    var url = app.data.httpUrl + 'v1/feed/list'
    var param = {
      pageNum: page + '',
    }
    httpTool.sendRequest(url, param, '加载动态中...', function (res) {
      var tempList = that.data.list
      if (page == 1) {
        tempList = res.page.items
      } else {
        for (var i = 0; i < res.page.items.length; i++) {
          var model = res.page.items[i];
          tempList.push(model)
        }
      }
      var isLastPage=false;
      if(page>=res.page.lastPage){
        isLastPage=true;
      }
      that.setData({
        list: tempList,
        isLoad: isLastPage,
      })
      if (that.data.list.length == 0) {
        that.setData({
          isLoad: 1
        })
      }
      console.log(that.data.list)
    }, function (error) {

    });

  },
  showAlbumClick: function (event) {

    var currentUrl = event.currentTarget.dataset.index;

    var list = event.currentTarget.dataset.list;
    console.log(list)
    console.log(currentUrl)
    var tempList = []
    for (var i = 0; i < list.length; i++) {
      var model = list[i]
      tempList.push(model.fileUrl)
    }
    wx.previewImage({
      current: currentUrl,
      urls: tempList,
    })
  },
  onHide: function() {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function() {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数

  },
  onShareAppMessage: function() {
    // 用户点击右上角分享
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  }
})